@echo off
title Property Cleaning System - Remote Anolis 7.9 Server Deployment Tool
color 2F

echo.
echo ============================================
echo   Property Cleaning System - Remote Deployment
echo   Target Server: 10.158.94.94
echo ============================================
echo.

:: Set variables
set SERVER_IP=10.158.94.94
set SSH_USER=gs
set SSH_PASS=www.Gsafety.com
set LOCAL_DIR=%~dp0
set REMOTE_DIR=/opt/property-cleaning-system

:: Check SSH connection
echo Checking SSH connection...
ssh %SSH_USER%@%SERVER_IP% -o ConnectTimeout=5 exit >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Cannot connect to server %SERVER_IP%
    echo.
    echo Please check:
    echo 1. Server IP address is correct
    echo 2. SSH service is running
    echo 3. Network connection is available
    echo 4. Firewall allows SSH port 22
    pause
    exit /b 1
)

echo SSH connection successful!
echo.

:: Create temporary script file
echo Preparing deployment files...
echo #!/bin/bash > deploy-temp.sh
echo set -e >> deploy-temp.sh
echo echo "Starting remote deployment..." >> deploy-temp.sh
echo >> deploy-temp.sh

:: Upload files using scp
echo Uploading deployment script...
scp deploy-remote-gs.sh %SSH_USER%@%SERVER_IP%:/tmp/
if %errorlevel% neq 0 (
    echo Error: Failed to upload deployment script
    pause
    exit /b 1
)

echo Uploading database files...
scp database/schema.sql %SSH_USER%@%SERVER_IP%:/tmp/
if %errorlevel% neq 0 (
    echo Error: Failed to upload database files
    pause
    exit /b 1
)

:: Execute remote deployment
echo.
echo Executing remote deployment script...
echo This may take a few minutes...
echo.

ssh %SSH_USER%@%SERVER_IP% "
    chmod +x /tmp/deploy-remote-gs.sh &&
    sudo /tmp/deploy-remote-gs.sh
"

if %errorlevel% neq 0 (
    echo Error: Remote deployment failed
    pause
    exit /b 1
)

echo.
echo ============================================
echo   Deployment completed successfully!
echo ============================================
echo.
echo Access URLs:
echo   Frontend: http://%SERVER_IP%:3000
echo   Backend API: http://%SERVER_IP%:8080
echo   API Documentation: http://%SERVER_IP%:8080/swagger-ui.html
echo.
echo Management commands (run on server):
echo   Check status: /opt/property-cleaning-system/check-status.sh
echo   Start system: /opt/property-cleaning-system/start-all.sh
echo   Backup data: /opt/property-cleaning-system/backup-db.sh
echo.
pause